import win32security
import ntsecuritycon as con
import pywintypes

def get_file_security(file_path):
    # 获取文件的安全对象
    sd = win32security.GetFileSecurity(file_path, win32security.DACL_SECURITY_INFORMATION)
    
    # 获取DACL（自由访问控制列表）
    dacl = sd.GetSecurityDescriptorDacl()
    
    print(f"Permissions for file: {file_path}")
    
    for i in range(0, dacl.GetAceCount()):
        ace = dacl.GetAce(i)
        try:
            user, _, _ = win32security.LookupAccountSid(None, ace[2])
        except pywintypes.error as e:
            print(f"pywintypes.error occurred: {e}")
            user = "Unknown User"
        access_mask = ace[1]
        
        # 解析权限
        permissions = []
        if access_mask & con.FILE_READ_DATA:
            permissions.append("READ")
        if access_mask & con.FILE_WRITE_DATA:
            permissions.append("WRITE")
        if access_mask & con.FILE_EXECUTE:
            permissions.append("EXECUTE")
        if access_mask & con.DELETE:
            permissions.append("DELETE")
        if access_mask & con.READ_CONTROL:
            permissions.append("READ_CONTROL")
        if not permissions:
            permissions.append("NO_PERMISSIONS_DETECTED")

        print(f"User/Group: {user}, Permissions: {', '.join(permissions)}")

def main():
    import sys
    if len(sys.argv) < 2:
        print("Usage: python script.py <file_path>")
        sys.exit(1)

    file_path = sys.argv[1]
    get_file_security(file_path)

if __name__ == "__main__":
    main()


# \\10.70.222.232\Public\华星大数据分析\大模型攻关会\日会\20250905\AI大模型周会进展_20250905.pptx
# 使用示例
#get_file_security("C:\\path\\to\\your\\file.txt")